// 问题数据
const questions = [
    {
        question: "发生火灾时，以下哪种逃生方式是正确的？",
        options: [
            "乘坐电梯快速下楼",
            "用湿毛巾捂住口鼻，弯腰快速撤离",
            "躲在床底下等待救援",
            "打开所有门窗通风"
        ],
        correct: 1,
        cartoon: "fire-safety"
    },
    {
        question: "遇到地震时，以下哪种做法是错误的？",
        options: [
            "躲在桌子底下",
            "远离窗户和玻璃",
            "立即跑到室外",
            "关闭电源和煤气"
        ],
        correct: 2,
        cartoon: "earthquake"
    },
    {
        question: "发现有人触电，首先应该怎么做？",
        options: [
            "立即用手拉触电者",
            "用金属棒挑开电线",
            "切断电源或使用绝缘物挑开电线",
            "泼水救人"
        ],
        correct: 2,
        cartoon: "electricity"
    },
    {
        question: "以下哪种情况容易引发火灾？",
        options: [
            "定期检查电器线路",
            "在厨房使用明火时离开",
            "保持消防通道畅通",
            "及时清理可燃物"
        ],
        correct: 1,
        cartoon: "fire-hazard"
    },
    {
        question: "遇到暴雨天气，以下哪种行为是安全的？",
        options: [
            "在树下避雨",
            "涉水过马路",
            "待在室内，远离窗户",
            "在空旷地带打伞"
        ],
        correct: 2,
        cartoon: "rain"
    },
    {
        question: "在公共场所遇到踩踏事件，以下哪种做法是正确的？",
        options: [
            "逆着人流方向快速奔跑",
            "站在原地等待救援",
            "顺着人流方向移动，保持身体平衡",
            "大声呼喊引起注意"
        ],
        correct: 2,
        cartoon: "crowd"
    },
    {
        question: "发现煤气泄漏时，以下哪种做法是错误的？",
        options: [
            "立即关闭煤气总阀",
            "打开门窗通风",
            "打开电灯查看情况",
            "撤离现场并报警"
        ],
        correct: 2,
        cartoon: "gas"
    },
    {
        question: "在野外遇到雷雨天气，以下哪种避雨方式最安全？",
        options: [
            "躲在大树下",
            "躲在金属棚下",
            "躲在低洼处",
            "寻找坚固的建筑物或山洞"
        ],
        correct: 3,
        cartoon: "thunder"
    }
];

let currentQuestion = 0;
let score = 0;

// 显示开始页面
function showStartPage() {
    document.getElementById('welcomePage').style.display = 'none';
    document.getElementById('startPage').style.display = 'block';
}

// 开始游戏
function startGame() {
    document.getElementById('startPage').style.display = 'none';
    document.getElementById('gamePage').style.display = 'block';
    showQuestion();
}

// 显示问题
function showQuestion() {
    const question = questions[currentQuestion];
    document.getElementById('question').textContent = question.question;
    
    // 设置卡通元素
    const cartoonElement = document.getElementById('cartoonElement');
    cartoonElement.style.backgroundImage = `url('./images/${question.cartoon}.png')`;
    
    const optionsContainer = document.getElementById('options');
    optionsContainer.innerHTML = '';
    
    question.options.forEach((option, index) => {
        const button = document.createElement('div');
        button.className = 'option';
        button.textContent = option;
        button.onclick = () => checkAnswer(index);
        optionsContainer.appendChild(button);
    });
    
    // 更新进度条
    const progress = ((currentQuestion + 1) / questions.length) * 100;
    document.getElementById('progress').style.width = `${progress}%`;
}

// 检查答案
function checkAnswer(selectedIndex) {
    const question = questions[currentQuestion];
    const options = document.querySelectorAll('.option');
    
    // 禁用所有选项
    options.forEach(option => option.style.pointerEvents = 'none');
    
    // 显示正确答案
    options[question.correct].classList.add('correct');
    
    if (selectedIndex === question.correct) {
        score++;
    } else {
        options[selectedIndex].classList.add('wrong');
    }
    
    // 延迟后进入下一题
    setTimeout(() => {
        currentQuestion++;
        if (currentQuestion < questions.length) {
            showQuestion();
        } else {
            showResult();
        }
    }, 1500);
}

// 显示结果
function showResult() {
    document.getElementById('gamePage').style.display = 'none';
    document.getElementById('resultPage').style.display = 'block';
    
    const finalScore = document.getElementById('finalScore');
    const percentage = (score / questions.length) * 100;
    
    let message = '';
    if (percentage >= 80) {
        message = `太棒了！你的安全意识很强！得分：${score}/${questions.length}`;
    } else if (percentage >= 60) {
        message = `不错！继续提升安全意识！得分：${score}/${questions.length}`;
    } else {
        message = `需要加强安全意识哦！得分：${score}/${questions.length}`;
    }
    
    finalScore.textContent = message;
}

// 分享结果
function shareResult() {
    const shareText = `我在安全知识大挑战中获得了${score}/${questions.length}分！快来测试你的安全意识吧！`;
    
    // 检查是否在微信环境中
    if (typeof WeixinJSBridge !== 'undefined') {
        WeixinJSBridge.invoke('shareTimeline', {
            title: '安全知识大挑战',
            desc: shareText,
            link: window.location.href,
            imgUrl: '分享图片URL' // 需要替换为实际的分享图片URL
        });
    } else {
        // 复制分享文本到剪贴板
        navigator.clipboard.writeText(shareText).then(() => {
            alert('分享文本已复制到剪贴板，请手动分享到朋友圈！');
        });
    }
} 